package com.sztx.pay.center.rpc.service;

import java.util.ArrayList;
import java.util.List;

import org.jsoup.helper.StringUtil;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.alibaba.fastjson.JSONObject;
import com.sztx.pay.center.common.annotation.SlaveQueryAnnotation;
import com.sztx.pay.center.common.util.Constance.CardType;
import com.sztx.pay.center.common.util.JfConfigUtil;
import com.sztx.pay.center.common.util.ObjectUtil;
import com.sztx.pay.center.common.util.ValidateUtil;
import com.sztx.pay.center.core.service.SettleService;
import com.sztx.pay.center.core.service.util.SettleTradeType;
import com.sztx.pay.center.core.service.util.SettleType;
import com.sztx.pay.center.rpc.api.domain.ResponsePageModel;
import com.sztx.pay.center.rpc.api.domain.SettleRuleListRequest;
import com.sztx.pay.center.rpc.api.domain.SettleRuleListResponse;
import com.sztx.pay.center.rpc.api.domain.SettleRuleRequest;
import com.sztx.pay.center.rpc.api.domain.SettleRuleResponse;
import com.sztx.pay.center.rpc.api.service.SettleRpcService;
import com.sztx.se.common.exception.BusinessException;
import com.sztx.se.dataaccess.mysql.config.PageQuery;

@Service
public class SettleRpcServiceImpl implements SettleRpcService {
	@Autowired
	SettleService settleService;
	
	@Override
	@SlaveQueryAnnotation
	public long calculateHandlingFee(int tradeType, int settleType, String userId, long amount) {
		return settleService.calculateHandlingFee(SettleTradeType.valueOf(tradeType), SettleType.valueOf(settleType), userId, amount, null,null,null);
	}
	
	@Override
	@SlaveQueryAnnotation
	public long calculateHandlingFee(int tradeType, int settleType, String userId, long amount,String channleId) {
		return settleService.calculateHandlingFee(SettleTradeType.valueOf(tradeType), SettleType.valueOf(settleType), userId, amount, channleId,null,null);
	}
	
	@Override
	@SlaveQueryAnnotation
	public long calculateHandlingFee(int tradeType, int settleType, String userId, long amount,String channleId ,String bankId,int cardType) {
		CardType cardTypeEnum = CardType.valueOf(cardType);
		ValidateUtil.notNull(cardTypeEnum, "cardType错误");
		return settleService.calculateHandlingFee(SettleTradeType.valueOf(tradeType), SettleType.valueOf(settleType), userId, amount, channleId,bankId,cardTypeEnum);
	}

	@Override
	public void configSettleRule(SettleRuleRequest req) {
		ValidateUtil.isBetween(req.getSettleType(), 1, 2, "结算类型错误");
		ValidateUtil.isBetween(req.getFeeType(), 1, 2, "手续费计费方式错误");
		ValidateUtil.isBetween(req.getState(), 1, 2, "状态错误");
		ValidateUtil.isNotBlank(req.getOperator(), "操作人不能为空");
		ValidateUtil.isNotBlank(req.getChannelId(), "渠道ID不能为空");
		ValidateUtil.isNotBlank(req.getUserId(), "用户帐号不能为空");
		if(req.getFeeAmount() < 0){
			throw new BusinessException("手续费金额/百分比不能小于0");
		}
		
		com.sztx.pay.center.core.service.bean.rpc.SettleRuleRequest request = new com.sztx.pay.center.core.service.bean.rpc.SettleRuleRequest();
		request = ObjectUtil.copyProperties(req, request);
		settleService.configSettleRule(request );
	}

	@Override
	@SlaveQueryAnnotation
	public SettleRuleResponse queryUserSettleRule(SettleRuleRequest req) {
		com.sztx.pay.center.core.service.bean.rpc.SettleRuleRequest request = new com.sztx.pay.center.core.service.bean.rpc.SettleRuleRequest();
		request = ObjectUtil.copyProperties(req, request);
		com.sztx.pay.center.core.service.bean.rpc.SettleRuleResponse settleRuleResponse = settleService.queryUserSettleRule(request);
		SettleRuleResponse response = new SettleRuleResponse();
		response = ObjectUtil.copyProperties(settleRuleResponse, response);
		return response;
	}

	@Override
	@SlaveQueryAnnotation
	public ResponsePageModel<SettleRuleListResponse> querySettleRuleList(
			SettleRuleRequest req,int pageNo,int pageSize) {
		ValidateUtil.gt(pageNo, 0, "页码不能小于1");
		ValidateUtil.gt(pageSize, 0, "页大小不能小于1");
		PageQuery pageQuery = new PageQuery(pageNo, pageSize);
		ResponsePageModel<SettleRuleListResponse> rep = new ResponsePageModel<SettleRuleListResponse>();
		
		com.sztx.pay.center.core.service.bean.rpc.SettleRuleRequest request = new com.sztx.pay.center.core.service.bean.rpc.SettleRuleRequest();
		request = ObjectUtil.copyProperties(req, request);
		Integer count = settleService.querySettleRuleCount(request);
		rep.setTotal(count);
		if(count < 1){
			return rep;
		}
		
		List<com.sztx.pay.center.core.service.bean.rpc.SettleRuleListResponse> querySettleRuleList = settleService.querySettleRuleList(request,pageQuery);
		List<SettleRuleListResponse> settleRuleList = new ArrayList<SettleRuleListResponse>();
//		for (com.sztx.pay.center.core.service.bean.rpc.SettleRuleListResponse settleRuleListResponse : querySettleRuleList) {
//			SettleRuleListResponse res = new SettleRuleListResponse();
//			res = ObjectUtil.copyProperties(settleRuleListResponse, res);
//			settleRuleList.add(res);
//		}
		settleRuleList = ObjectUtil.copyProperties(querySettleRuleList, settleRuleList);
		rep.setResultLists(settleRuleList);
		return rep;
	}

	@Override
	public void configSettleRules(SettleRuleListRequest list) {
		String userId = list.getUserId();
		String channelId = list.getChannelId();
		int type;
		if(!StringUtil.isBlank(userId)){
			type = 1;
		}else if(!StringUtil.isBlank(channelId)){
			type = 2;
		}else{
			type = 3;
		}
		com.sztx.pay.center.core.service.bean.rpc.SettleRuleListRequest list2 = new com.sztx.pay.center.core.service.bean.rpc.SettleRuleListRequest();
		
		list2 = ObjectUtil.copyProperties(list, list2);
		
		
		settleService.configSettleRules(list2 , type);
	}

	
	

	@Override
	public void cancleRule(String channelId, String userId) {
		settleService.cancleRule(channelId, userId);
	}

	@Override
	public void reloadJfConfig() {
		JfConfigUtil.reload();
	}

	@Override
	public void settlingOrders() {
		settleService.settlingOrders();
	}

	
}
